package com.csq.androidproject.utils;

import hirondelle.date4j.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.csq.androidproject.views.calendarview.caldroid.Weekday;

public class DateUtils {
    
    public static ArrayList<String> getDaysOfWeek(int startDayOfWeek) {
        ArrayList<String> list = new ArrayList<String>();

        // 17 Feb 2013 is Sunday
        DateTime sunday = new DateTime(2013, 2, 17, 0, 0, 0, 0);
        DateTime nextDay = sunday.plusDays(startDayOfWeek - Weekday.SUNDAY);

        for (int i = 0; i < 7; i++) {
            list.add(nextDay.format("WWW", Locale.getDefault()).toUpperCase());
            nextDay = nextDay.plusDays(1);
        }

        return list;
    }

    /**
     * Retrieve all the dates for a given calendar month Include previous month,
     * current month and next month.
     * 
     * @param month
     * @param year
     * @param startDayOfWeek
     *            : calendar can start from customized date instead of Sunday
     * @return
     */
    public static SimpleDateFormat yyyyMMddFormat = new SimpleDateFormat(
            "yyyy-MM-dd", Locale.ENGLISH);

    public static ArrayList<DateTime> getFullWeeks(int month, int year,int startDayOfWeek) {
        ArrayList<DateTime> datetimeList = new ArrayList<DateTime>();

        /*DateTime firstDateOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
        DateTime lastDateOfMonth = firstDateOfMonth.getEndOfMonth();

        // Add dates of first week from previous month
        int weekdayOfFirstDate = firstDateOfMonth.getWeekDay();

        // If weekdayOfFirstDate smaller than startDayOfWeek
        // For e.g: weekdayFirstDate is Monday, startDayOfWeek is Tuesday
        // increase the weekday of FirstDate because it's in the future
        if (weekdayOfFirstDate < startDayOfWeek) {
            weekdayOfFirstDate += 7;
        }

        while (weekdayOfFirstDate > 0) {
            DateTime dateTime = firstDateOfMonth.minusDays(weekdayOfFirstDate - startDayOfWeek);
            if (!dateTime.lt(firstDateOfMonth)) {
                break;
            }

            datetimeList.add(dateTime);
            weekdayOfFirstDate--;
        }

        // Add dates of current month
        for (int i = 0; i < lastDateOfMonth.getDay(); i++) {
            datetimeList.add(firstDateOfMonth.plusDays(i));
        }

        // Add dates of last week from next month
        int endDayOfWeek = startDayOfWeek - 1;

        if (endDayOfWeek == 0) {
            endDayOfWeek = 7;
        }

        if (lastDateOfMonth.getWeekDay() != endDayOfWeek) {
            int i = 1;
            while (true) {
                DateTime nextDay = lastDateOfMonth.plusDays(i);
                datetimeList.add(nextDay);
                i++;
                if (nextDay.getWeekDay() == endDayOfWeek) {
                    break;
                }
            }
        }*/
        
        //修改为一直返回42天
        //找到要添加的第一天
        DateTime firstDateOfMonth = new DateTime(year, month, 1, 0, 0, 0, 0);
        int weekdayOfFirstDate = firstDateOfMonth.getWeekDay();

        if (weekdayOfFirstDate < startDayOfWeek) {
            weekdayOfFirstDate += 7;
        }
        //要添加的第一天
        DateTime firstAddTime = firstDateOfMonth.minusDays(weekdayOfFirstDate - startDayOfWeek);
        for(int i = 0 ; i < 42 ; i++){
            datetimeList.add(firstAddTime.plusDays(i));
        }

        return datetimeList;
    }

    /**
     * Get the DateTime from Date, with hour and min is 0
     * 
     * @param date
     * @return
     */
    public static DateTime convertDateToDateTime(Date date) {
        DateTime dateTime = new DateTime(yyyyMMddFormat.format(date));
        dateTime = new DateTime(dateTime.getYear(), dateTime.getMonth(),
                dateTime.getDay(), 0, 0, 0, 0);
        return dateTime;
    }

    public static Date convertDateTimeToDate(DateTime dateTime) {
        String dateString = dateTime.format("YYYY-MM-DD");
        try {
            return getDateFromString(dateString, null);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Get the Date from String with custom format. Default format is yyyy-MM-dd
     * 
     * @param dateString
     * @param dateFormat
     * @return
     * @throws ParseException
     */
    public static Date getDateFromString(String dateString, String dateFormat)
            throws ParseException {
        SimpleDateFormat formatter;
        if (dateFormat == null) {
            formatter = yyyyMMddFormat;
        } else {
            formatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
        }

        return formatter.parse(dateString);
    }

    /**
     * Get the DateTime from String with custom format. Default format is
     * yyyy-MM-dd
     * 
     * @param dateString
     * @param dateFormat
     * @return
     */
    public static DateTime getDateTimeFromString(String dateString,
            String dateFormat) {
        Date date;
        try {
            date = getDateFromString(dateString, dateFormat);
            return convertDateToDateTime(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static ArrayList<String> convertToStringList(
            ArrayList<DateTime> dateTimes) {
        ArrayList<String> list = new ArrayList<String>();
        for (DateTime dateTime : dateTimes) {
            list.add(dateTime.format("YYYY-MM-DD"));
        }
        return list;
    }
    
    public static String getYMDString(DateTime data){
        return data.format("YYYY-MM-DD");
    }
    
    public static DateTime getCurrentDataTime(){
        return convertDateToDateTime(Calendar.getInstance().getTime());
    }

}
